package com.czxy.demo06;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hp on 2020/2/26.
 */
public class Test06 {
    public static void main(String[] args) throws DocumentException {
        Map<String, Object> stringObjectMap = queryList();
        for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
            System.out.println(stringObjectEntry);
        }
    }
    public static Map<String,Object> queryList() throws DocumentException {
        Map<String,Object> mapmap = new HashMap<>();;
        int count = 1;

        List<Element> elements = new SAXReader().read(new File("areainfo.xml")).getRootElement().elements();
        List<Map<String,Object>> list1 = new ArrayList<>();
        for (Element element : elements) {
            Map<String,Object> map1 = new HashMap<>();

            List<Map<String,Object>> list2 = new ArrayList<>();
            String pid = element.attributeValue("pid");
            String pname = element.attributeValue("pname");



            List<Element> ciddd = element.elements();
            for (Element cidd : ciddd) {

                List<Map<String,String>> list3 = new ArrayList<>();
                Map<String,Object> map2 = new HashMap<>();
                String cid = cidd.attributeValue("cid");
                String cname = cidd.attributeValue("cname");

                List<Element> countytown = cidd.elements();
                for (Element coun : countytown) {

                    Map<String,String> map3 = new HashMap<>();
                    String tid = coun.attributeValue("tid");
                    String tname = coun.attributeValue("tname");
                    map3.put("tid",tid);
                    map3.put("tname",tname);
                    list3.add(map3);


                }
                map2.put("countytownList",list3);
                map2.put("cid",cid);
                map2.put("cname",cname);
                list2.add(map2);

            }
            map1.put("pid",pid);
            map1.put("pname",pname);
            map1.put("cityList",list2);
            list1.add(map1);


        }
        mapmap.put("prolist"+count++,list1);
        return mapmap;
    }
}
